/**
 * Project		: Nerdle
 * File 		: StatisticalAnalyser.java			Version: 
 * Date 		: 2:08:39 AM
 * Author		: kkopiec
 * Description	:
 *
 */

/**
 * @author kkopiec
 *
 */
import java.util.*;

public class StatisticalAnalyser {

	protected HashMap<Character, Integer> analysis = new HashMap<Character, Integer>();
	protected ArrayList<String> result = new ArrayList<String>();
	protected ArrayList<Integer> subtotals = new ArrayList<Integer>();
	Double total = new Double(0);

	public void analyze(ArrayList<String> words) {
		for (String word : words) {
			for (int i = 0; i < word.length(); i++) {
				Character c = new Character(word.charAt(i));
				if (analysis.containsKey(c)) {
					Integer num = ((Integer) analysis.get(c)).intValue();
					num += 1;
					analysis.put(c, num);
				} else {
					analysis.put(c, new Integer(1));
				}
			}
		}
	}
	public Map<Character, Integer> DistStats(){
		return analysis;
	}
	public Double totalChars(){
		Set<Map.Entry<Character, Integer>> set = analysis.entrySet();
		Iterator<Map.Entry<Character, Integer>> i = set.iterator();
		while (i.hasNext()) {
			Map.Entry<Character, Integer> me = (Map.Entry<Character, Integer>) i.next();
			total = total + me.getValue();
		}
		return total; 
	}
/**
 * Displays analysis for debug proces only ...
 */
	public void displayAnalysis() {
		Set<Map.Entry<Character, Integer>> set = analysis.entrySet();
		Iterator<Map.Entry<Character, Integer>> i = set.iterator();
		while (i.hasNext()) {
			Map.Entry<Character, Integer> me = (Map.Entry<Character, Integer>) i
					.next();
			result.add(me.getKey() + " --> " + me.getValue() + " ---> % ");
			total = total + me.getValue();
			subtotals.add(me.getValue());
			// System.out.println(me.getKey() + " --> " + me.getValue());

		}

		Double test = new Double(0);
		int counter = 0 ;
		for (String s : result) {
			
			Double immediate = subtotals.remove(0) * 100 / total;
			test += immediate;
			s = s + immediate;
			result.set(counter, s);
			counter++;

		}
		Collections.sort(result);
		for (String s : result) {

			System.out.println(s);

		}
		System.out.println("Test of immediates (supposed to be ~100): " + test);
	}
}
